**Code for "China in Africa: More business-as-usual than bogeyman, Bond villain, or benefactor"
*manucript for Security in Context
*Collin Meisel, 1 November 2024

	
********** Data prep
		cd
		use "Diplometrics_bilateral-data-for-SiC-analysis_20241101.dta", clear						
		encode countrya, gen(countrya1)
		encode countryb, gen(countryb1)
		gen undyada = cond(countrya1 < countryb1, countrya1, countryb1)
		gen undyadb = cond(countrya1 < countryb1, countryb1, countrya1)
		egen dyadid = group(undyada undyadb)
		gen gdpmerbil_a = gdpmer2011usda/1000000000
		gen gdpmerbil_b = gdpmer2011usdb/1000000000
		gen totaltradeawithb_bil = totaltradeawithb/1000000000

		egen ayear = group(countrya1 year)
		egen byear = group(countryb1 year)

********* Analysis

*trade		
		
		global varlist1 l.powershare_a l.embassylorainb l.embassylorbina l.atopally l.tradeagreement_rta l.armsexportsatob l.polyarchyaffinity l.unga_coincidence
		xtset directeddyadid year
		*unstandardized regression
		ppmlhdfe totaltradeawithb l.totaltradeawithb_bil $varlist1, a(dyadid ayear byear) d(one)
		predict phat1
		gen diff = totaltradeawithb - phat1 /*postive = trading more than expected, negative = less than expected*/
		hist diff if countrya=="China" & unregionb=="Africa", freq
		egen countryaregionbyear = group(countrya unregionb year)
		egen diff_regionavg = mean(diff),by(countryaregionbyear)
		twoway(line diff_regionavg year if countrya=="China" & countryb=="South Africa")
		egen diff_regiontotal = total(diff),by(countryaregionbyear)
		twoway(line diff_regiontotal year if countrya=="China" & countryb=="South Africa")(line diff_regiontotal year if countrya=="United States" & countryb=="South Africa")	

		gen diff_pct = (totaltradeawithb/phat1)*100
		egen diff_pct_regionavg = mean(diff_pct),by(countryaregionbyear)
		
*fdi		
		
		global varlist2 l.powershare_a l.embassylorainb l.embassylorbina l.atopally l.tradeagreement_rta l.armsexportsatob l.polyarchyaffinity l.unga_coincidence l.totaltradeawithb
		
		ppmlhdfe fdioutstocks2011usdatob l.fdioutstocks2011usdatob $varlist2, a(dyadid ayear byear) d(two)
		predict phat2
		gen diff_invest = fdioutstocks2011usdatob - phat2 /*postive = more than expected, negative = less than expected*/
		egen diff_invest_regionavg = mean(diff_invest),by(countryaregionbyear)
		twoway(line diff_invest_regionavg year if countrya=="China" & countryb=="South Africa")
		egen diff_invest_regiontotal = total(diff_invest),by(countryaregionbyear)
		twoway(line diff_invest_regiontotal year if countrya=="China" & countryb=="South Africa")(line diff_invest_regiontotal year if countrya=="United States" & countryb=="South Africa")	
		
		gen diff_invest_pct = (fdioutstocks2011usdatob/phat2)*100
		egen diff_invest_pct_regionavg = mean(diff_invest_pct),by(countryaregionbyear)		
				
*aid		
		
		global varlist4 l.powershare_a l.embassylorainb l.embassylorbina l.atopally l.tradeagreement_rta l.armsexportsatob l.polyarchyaffinity l.unga_coincidence l.totaltradeawithb
		
		ppmlhdfe foreignaid2011usdatob l.foreignaid2011usdatob $varlist4, a(dyadid ayear byear) d(four)
		predict phat4
		gen diff_aid = foreignaid2011usdatob - phat4 /*postive = more than expected, negative = less than expected*/
		egen diff_aid_regionavg = mean(diff_aid),by(countryaregionbyear)
		twoway(line diff_aid_regionavg year if countrya=="China" & countryb=="South Africa")
		egen diff_aid_regiontotal = total(diff_aid),by(countryaregionbyear)
		twoway(line diff_aid_regiontotal year if countrya=="China" & countryb=="South Africa")(line diff_aid_regiontotal year if countrya=="United States" & countryb=="South Africa")	

		gen diff_aid_pct = (foreignaid2011usdatob/phat4)*100
		egen diff_aid_pct_regionavg = mean(diff_aid_pct),by(countryaregionbyear)	
		replace diff_aid_pct_regionavg=. if diff_aid_pct_regionavg==0
*oof	
		
		global varlist5 l.powershare_a l.embassylorainb l.embassylorbina l.atopally l.tradeagreement_rta l.armsexportsatob l.polyarchyaffinity l.unga_coincidence l.totaltradeawithb
		
		ppmlhdfe oofaid2011usdatob l.oofaid2011usdatob $varlist5, a(dyadid ayear byear) d(five)
		predict phat5
		gen diff_oof = oofaid2011usdatob - phat5 /*postive =  more than expected, negative = less than expected*/
		egen diff_oof_regionavg = mean(diff_oof),by(countryaregionbyear)
		twoway(line diff_oof_regionavg year if countrya=="China" & countryb=="South Africa")
		egen diff_oof_regiontotal = total(diff_oof),by(countryaregionbyear)
		twoway(line diff_oof_regiontotal year if countrya=="China" & countryb=="South Africa")(line diff_oof_regiontotal year if countrya=="United States" & countryb=="South Africa")	

		gen diff_oof_pct = (oofaid2011usdatob/phat5)*100
		egen diff_oof_pct_regionavg = mean(diff_oof_pct),by(countryaregionbyear)	

*Graph - Percent differences in observations vs. expectations for Chinese engagement in Africa on average	
		twoway(line diff_pct_regionavg year if countrya=="China" & countryb=="South Africa" & year>1999)(line diff_pct_regionavg year if countrya=="United States" & countryb=="South Africa" & year>1999)/*	
		*/(line diff_invest_pct_regionavg year if countrya=="China" & countryb=="South Africa" & year>1999)(line diff_invest_pct_regionavg year if countrya=="United States" & countryb=="South Africa" & year>1999)/*	
		*/(line diff_aid_pct_regionavg year if countrya=="China" & countryb=="South Africa" & year>1999, cmissing(n))(line diff_aid_pct_regionavg year if countrya=="United States" & countryb=="South Africa" & year>1999)/*	
		*/(line diff_oof_pct_regionavg year if countrya=="China" & countryb=="South Africa" & year>1999)(line diff_oof_pct_regionavg year if countrya=="United States" & countryb=="South Africa" & year>1999),/*	
		*/legend(position(3)cols(1)order(1 3 5 7 2 4 6 8)label(1 "China's trade")label(2 "US trade")label(3 "China's FDI")label(4 "US FDI")label(5 "China's ODA")label(6 "US ODA")label(7 "China's OOF")label(8 "US OOF"))/*
		*/yline(100,lwidth(thick)lcolor(gs10)) ylabel(,angle(0)) ytitle(Percent of expected value)/*
		*/xlabel(2000 2004 2008 2012 2016 2020 2024) xtitle("") text(75 2025 "FDI = foreign direct investment" "ODA = official development assistance" "OOF = other official flows",placement(east) size(vsmall)just(left))
		
*Graph not shown - scatter plot of Formal Bilateral Influence Capacity in Africa and electoral democracy
		scatter fbic v2x_polyarchy_b if countrya=="China" & unregionb=="Africa" /*
		*/& year==2023 || scatter fbic v2x_polyarchy_b if countrya=="United States" /*
		*/& unregionb=="Africa" & year==2023, legend(label(1 "China")label(2 "US")) /*
		*/ylabel(,ang(horizontal)) ytitle("Formal Bilateral Influence Capacity Index")/*
		*/ xtitle("Electoral Democracy Index") text(0.25 0.05 "China: r = 0.037",place(e)) /*
		*/text(0.25 0.6 "US: r = -0.063",place(e))
